package com.atguigu.service_edu.service.impl;

import com.atguigu.service_edu.entity.EduTeacher;
import com.atguigu.service_edu.mapper.EduTeacherMapper;
import com.atguigu.service_edu.entity.vo.EduTeacherQuery;
import com.atguigu.service_edu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author clark
 * @since 2020-04-01
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public void pageQuery(Page<EduTeacher> pagePram, EduTeacherQuery eduTeacherQuery) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");

        if(eduTeacherQuery == null){
            baseMapper.selectPage(pagePram,queryWrapper);
            return;
        }

        String name = eduTeacherQuery.getName();
        Integer level = eduTeacherQuery.getLevel();
        String begin = eduTeacherQuery.getBegin();
        String end = eduTeacherQuery.getEnd();

        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if(level != null){
            queryWrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            queryWrapper.le("gme_create",end);
        }

        baseMapper.selectPage(pagePram,queryWrapper);

    }


    @Cacheable(value = "teacher",key="'listLimit4'")
    @Override
    public List<EduTeacher> listLimit4() {
        QueryWrapper<EduTeacher> eduTeacherQueryWrapper = new QueryWrapper<>();
        eduTeacherQueryWrapper.orderByDesc("id");
        eduTeacherQueryWrapper.last("limit 4");
        List<EduTeacher> eduTeacherList = baseMapper.selectList(eduTeacherQueryWrapper);
        return eduTeacherList;
    }

    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> pageTeacher) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //把分页数据封装到pageTeacher对象中
        baseMapper.selectPage(pageTeacher, wrapper);
        List<EduTeacher> records = pageTeacher.getRecords();
        long pages = pageTeacher.getPages();
        long total = pageTeacher.getTotal();
        long size = pageTeacher.getSize();
        long current = pageTeacher.getCurrent();
        boolean hasPrevious = pageTeacher.hasPrevious();
        boolean hasNext = pageTeacher.hasNext();

        Map<String,Object> map = new HashMap<>();
        map.put("items",records);
        map.put("total",total);
        map.put("pages",pages);
        map.put("size",size);
        map.put("current",current);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        return map;
    }
}
